package alogrithm;

/**
 * @author lbw
 * @version 1.0
 * @date 2021/10/18 12:39
 */


public class MergeLinkedList {
    public ListNode Merge(ListNode list1, ListNode list2) {

        ListNode dummyNode = new ListNode(-1);
        ListNode curr = dummyNode;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                curr.next = list1;
                list1 = list1.next;
            } else {
                curr.next = list2;
                list2 = list2.next;
            }
            curr = curr.next;
        }
        curr.next = list1 != null ? list1 : list2;
        return dummyNode.next;
    }

    class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}
